package com.example.cr.insect.service;

import cn.hutool.core.bean.BeanUtil;
import com.example.cr.common.response.PageResponse;
import com.example.cr.insect.entity.Pest;
import com.example.cr.insect.entity.PestExample;
import com.example.cr.insect.mapper.PestMapper;
import com.example.cr.insect.request.PestListRequest;
import com.example.cr.insect.response.PestResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
    import cn.hutool.core.date.DateTime;
    import cn.hutool.core.util.ObjectUtil;
    import com.example.cr.common.util.SnowflakeUtil;
    import com.example.cr.insect.request.PestRequest;

@Service
public class PestService {
private static final Logger log = LoggerFactory.getLogger(PestService.class);
@Autowired
PestMapper pestMapper;

public PageResponse<PestResponse> list(PestListRequest request) {
    PestExample pestExample = new PestExample();
    pestExample.setOrderByClause("id desc");

    // 处理搜索条件
    PestExample.Criteria criteria = pestExample.createCriteria();
    if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
    String keyword = "%" + request.getKeyword() + "%";
                criteria.andNameLike(keyword);
                PestExample.Criteria criteria2 = pestExample.createCriteria();
                criteria2.andScientificNameLike(keyword);
                pestExample.or(criteria2);
                PestExample.Criteria criteria3 = pestExample.createCriteria();
                criteria3.andCategoryLike(keyword);
                pestExample.or(criteria3);
    }

    PageHelper.startPage(request.getPage(), request.getSize());
    log.info("当前页码 current_page = {}", request.getPage());
    log.info("每页记录数 per_page = {}", request.getSize());

    List<Pest> pests = pestMapper.selectByExample(pestExample);

    PageInfo<Pest> pageInfo = new PageInfo<>(pests);
    log.info("总记录数 total = {}", pageInfo.getTotal());
    log.info("总页数 pages = {}", pageInfo.getPages());

    List<PestResponse> list = BeanUtil.copyToList(pests, PestResponse.class);

        PageResponse<PestResponse> pageResponse = new PageResponse<>();
            pageResponse.setTotal(pageInfo.getTotal());
            pageResponse.setList(list);

            return pageResponse;
            }
            public void save(PestRequest request) {
            Pest pest = BeanUtil.copyProperties(request, Pest.class);
            DateTime now = DateTime.now();
            if (ObjectUtil.isNull(pest.getId()) || pest.getId() == 0L) {
            pest.setId(SnowflakeUtil.getId());
            pest.setCreatedAt(now);
            pest.setUpdatedAt(now);
            pestMapper.insert(pest);
            } else {
            pest.setUpdatedAt(now);
            pestMapper.updateByPrimaryKeySelective(pest);
            }
            }

            public int deleteBatch(List<Long> ids) {
                PestExample pestExample = new PestExample();
                PestExample.Criteria criteria = pestExample.createCriteria();
                criteria.andIdIn(ids);
                return pestMapper.deleteByExample(pestExample);
                }
                }
